﻿using Dapper;
using System;
using BCEntity.OA.WorkSchedule.WorkSchedule;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCCommon.OAEnums.WorkSchedule;

namespace BCData.OA.WorkSchedule.WorkSchedule
{
	/// <summary>
	/// 工作进度表
	/// </summary>
	public class WorkScheduleData : IWorkScheduleData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM oa_workschedule";
		private const string QUERY = @"SELECT WorkScheduleId,EndDateTime,CreateTime,CreateAdminId,CreateAdminName,EditAdminId,EditAdminName,UpdateTime,ProcessStatus,CompanyId,CompanyName,DepartmentId,DepartmentName,ScheduleTitle,ScheduleType,ScheduleRemark,StartDate,EndDate,StartDateTime FROM oa_workschedule";
		public WorkScheduleData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查工作进度表是否存在
		public async Task<bool> Exists(long workScheduleId)
		{
			string sql = string.Format(@"{0} WHERE WorkScheduleId=?WorkScheduleId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkScheduleId", workScheduleId, DbType.Int64);
			return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加工作进度表
		public async Task<WorkScheduleEntity> Add(WorkScheduleEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("oa_workschedule", new string[] { "CreateAdminId", "CreateAdminName", "CompanyId", "CompanyName", "DepartmentId", "DepartmentName", "ScheduleTitle", "ScheduleType", "ScheduleRemark", "EndDateTime", "StartDate", "EndDate", "StartDateTime" });
			sql = string.Format("{0} {1} WHERE WorkScheduleId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
			parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?CompanyId", entity.CompanyId, DbType.Int32, size: 11);
			parameters.Add("?CompanyName", entity.CompanyName?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?DepartmentId", entity.DepartmentId, DbType.Int32, size: 11);
			parameters.Add("?DepartmentName", entity.DepartmentName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?ScheduleTitle", entity.ScheduleTitle?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?ScheduleType", entity.ScheduleType.ToString(), DbType.AnsiString);
			parameters.Add("?ScheduleRemark", entity.ScheduleRemark?.Trim(), DbType.String);
			parameters.Add("?StartDate", entity.StartDate, DbType.DateTime);
			parameters.Add("?StartDateTime", entity.StartDateTime, DbType.DateTime);
			parameters.Add("?EndDate", entity.EndDate, DbType.DateTime);
			parameters.Add("?EndDateTime", entity.EndDateTime, DbType.DateTime);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkScheduleEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改工作进度表
		public async Task<WorkScheduleEntity> Update(WorkScheduleEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("oa_workschedule", new string[] { "EndDateTime", "EditAdminId", "EditAdminName", "ProcessStatus", "CompanyId", "CompanyName", "DepartmentId", "DepartmentName", "ScheduleTitle", "ScheduleType", "ScheduleRemark", "StartDate", "EndDate", "StartDateTime" }, new string[] { "WorkScheduleId" });
			sql = string.Format("{0} {1} WHERE WorkScheduleId=?WorkScheduleId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkScheduleId", entity.WorkScheduleId, DbType.Int64, size: 20);
			parameters.Add("?EndDateTime", entity.EndDateTime, DbType.DateTime);
			parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?ProcessStatus", entity.ProcessStatus.ToString(), DbType.AnsiString);
			parameters.Add("?CompanyId", entity.CompanyId, DbType.Int32, size: 11);
			parameters.Add("?CompanyName", entity.CompanyName?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?DepartmentId", entity.DepartmentId, DbType.Int32, size: 11);
			parameters.Add("?DepartmentName", entity.DepartmentName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?ScheduleTitle", entity.ScheduleTitle?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?ScheduleType", entity.ScheduleType.ToString(), DbType.AnsiString);
			parameters.Add("?ScheduleRemark", entity.ScheduleRemark?.Trim(), DbType.String);
			parameters.Add("?StartDate", entity.StartDate, DbType.DateTime);
			parameters.Add("?EndDate", entity.EndDate, DbType.DateTime);
			parameters.Add("?StartDateTime", entity.StartDateTime, DbType.DateTime);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkScheduleEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除工作进度表
		public async Task<bool> Delete(long workScheduleId)
		{
			string sql = @"DELETE FROM oa_workschedule WHERE WorkScheduleId=?WorkScheduleId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkScheduleId", workScheduleId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取工作进度表
		public async Task<WorkScheduleEntity> GetEntity(long workScheduleId)
		{
			string sql = string.Format(@"{0} WHERE WorkScheduleId=?WorkScheduleId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkScheduleId", workScheduleId, DbType.Int64);
			return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkScheduleEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作进度表列表
		public async Task<IEnumerable<WorkScheduleEntity>> GetList(int? companyId, int? departmentId, OAWorkScheduleType scheduleType, DateTime start, DateTime end)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();
			wheres.Add("ScheduleType=?ScheduleType");
			parameters.Add("?ScheduleType", scheduleType.ToString(), DbType.AnsiString);
			if (companyId.HasValue)
			{
				wheres.Add("CompanyId=?CompanyId");
				parameters.Add("?CompanyId", companyId.Value, DbType.Int32, size: 11);
			}
			if (departmentId.HasValue)
			{
				wheres.Add("DepartmentId=?DepartmentId");
				parameters.Add("?DepartmentId", departmentId.Value, DbType.Int32, size: 11);
			}
			string sql = string.Format(@"{0} {1}", QUERY, wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "");
			return await this.databaseContext.Slave.QueryAsync<WorkScheduleEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作进度表分页
		public async Task<(IEnumerable<WorkScheduleEntity>, int)> Query(int? companyId, int? departmentId, OAWorkScheduleType scheduleType, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();
			wheres.Add("ScheduleType=?ScheduleType");
			parameters.Add("?ScheduleType", scheduleType.ToString(), DbType.AnsiString);
			if (companyId.HasValue)
            {
				wheres.Add("CompanyId=?CompanyId");
				parameters.Add("?CompanyId", companyId.Value, DbType.Int32, size: 11);
			}
			if (departmentId.HasValue)
			{
				wheres.Add("DepartmentId=?DepartmentId");
				parameters.Add("?DepartmentId", departmentId.Value, DbType.Int32, size: 11);
			}
			if (start.HasValue && !end.HasValue)
			{
				wheres.Add(string.Format("{0}>=?Start", scheduleType == OAWorkScheduleType.Date?"StartDate": "StartDateTime"));
				parameters.Add("?Start", start.Value, scheduleType == OAWorkScheduleType.Date ?DbType.Date: DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add(string.Format("{0}<=?End", scheduleType == OAWorkScheduleType.Date ? "EndDate" : "EndDateTime"));
				parameters.Add("?End", end.Value, scheduleType == OAWorkScheduleType.Date ? DbType.Date : DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add(string.Format("{0} >=?Start AND {1}<=?End", scheduleType == OAWorkScheduleType.Date ? "StartDate" : "StartDateTime", scheduleType == OAWorkScheduleType.Date ? "EndDate" : "EndDateTime"));
				parameters.Add("?Start", start.Value, scheduleType == OAWorkScheduleType.Date ? DbType.Date : DbType.DateTime);
				parameters.Add("?End", end.Value, scheduleType == OAWorkScheduleType.Date ? DbType.Date : DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM oa_workschedule {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.Slave.QueryMultipleAsync<WorkScheduleEntity, int>(sql, parameters);
		}
		#endregion
	}
}



